<?php
header ( "Content-type:text/html;charset=utf-8" );
class IndexAction extends Action{
	// 首页
	public function index()
    {

       $video= M("article");

       $rsvideo=$video->where(' tid = 19')->limit("6")->order('id desc')->findAll();//演奏

       $rsyuepu=$video->where(' tid = 23')->limit("6")->order('id desc')->findAll();//乐谱

       $rsshou=$video->where('tid = 20')->limit("20")->order('id desc')->findAll();//小号手

       $rsjiaoxue=$video->where('tid = 21')->limit("12")->order('rand()')->findAll();//教学

       $rsdaquan=$video->where('tid = 24')->limit("8")->order('id desc')->findAll();//大全

       $rsg=$video->limit("26")->order('id desc')->findAll();//更新


        $this->assign('video',$rsvideo);
        $this->assign('yuepu',$rsyuepu);
        $this->assign('shou',$rsshou);
        $this->assign('jiaoxue',$rsjiaoxue);
        $this->assign('quan',$rsdaquan);

         $this->assign('news',$rsg);


		$this->display(index);
	}
    //后台登陆验证
    public function checkadmin()
    {
          $name=$_POST['name'];
          $pass=$_POST['pass'];



          if($name=='')
        {
               $this->assign('jumpUrl', '__APP__/Index/admincp');
           $this->error('用户名不能为空');
        }
        if($pass=='')
        {
             $this->assign('jumpUrl', '__APP__/Index/admincp');
           $this->error('密码不能为空');
        }

        if($_SESSION['verify'] == '') {
                $this->error('验证码为空');
        }

        if($_SESSION['verify'] != md5($_POST['verify'])) {
                $this->error('验证码错误');
        }

        $Member	=	M("user");
        $rs = $Member->query(" select * from  ns_user where name='".$name."' and pass='".md5($pass)."' ");



   if($rs == false){
        $this->assign('jumpUrl', '__APP__/Index/admin');
     $this->error('登录失败');
   }
   else
        {

       $_SESSION['ssauth']='fuckworld';
       $this->assign('jumpUrl', '__APP__/Admin');

       $this->success('登录成功！');

   }

    }

public function upclick()
    {
        
        $id=RemoveXSS($_GET['id']);
        $Model = new Model();
        $Model->execute("update ns_article set click=click+1 where id = ".$id);


        
    }

	// 显示普通文章内容页
	public function show()
    {
        //echo $_GET['id'];
        $id=RemoveXSS($_GET['id']);
        if(!empty($id))
        {


            $type= M("article");

            $tid=$type->field("tid")->where(" id =  ".$id)->find();
            
            //dump($tid['tid']);


            $typedao=M("type");
            $ttid=$typedao->field("name")->where(" id = ".$tid['tid'])->find();

            $vs= $type->where(" id = ".$id)->find();








            //评论数及分页
            $Comm= M ('comments');
         

            $rs=$Comm->where('aid='.$id)->order('id desc')->findAll();

          
            $this->assign("fenleim",getfenlei($tid['tid']));
            $this->assign("ttid",$ttid);
            $this->assign("tttid",$tid['tid']);
            $this->assign("nav",$tid['tid']);




            //相关文章

            $rd=$type->order("id desc")->findAll();
            $xiangsi = array();
            foreach ($rd as $value){
            
               similar_text($value['title'],$vs['title'],$percent); //比较相似度 存放于$percent
               if($percent>40 && $percent<100){$xiangsi[]=$value;}
            
            }
            
            //dump(count($xiangsi));

            $this->assign("rd1",$xiangsi);

            //热点文章

            $rd2=$type->where(" tid = ".$tid['tid'])->order("click desc")->limit("10")->findAll();

            $this->assign("rd2",$rd2);


            
            
            
            $this->assign("vsfan",$vs['fan']);






             //上一篇下一篇

        $rsnext=$type->field("id,title,tid")->where('id >'.$id)->order('id asc')->find();




        $vnext=array ("id"=>$rsnext["id"],"title"=>$rsnext["title"],"tid"=>$rsnext["tid"]);


       $rsprev=$type->field("id,title,tid")->where('id <'.$id)->order('id desc')->find();



       $vprev=array ("id"=>$rsprev["id"],"title"=>$rsprev["title"],"tid"=>$rsprev["tid"]);

       import ( 'ORG.Util.SplitWord' );
       $sp = new SplitWord();
       $vkeyword=$vs['title'];
       $vkeyword=preg_replace('/\s/','',preg_replace("/[[:punct:]]/",'',strip_tags(html_entity_decode(str_replace(array('？','！','￥','（','）','：','‘','’','“','”','《','》','，','…','。','、','nbsp','·'),'',$vkeyword),ENT_QUOTES,'UTF-8'))));
       //echo $vkeyword;exit;



       $vkeyword=$sp->SplitRMM(utf2gb($vkeyword));
       $sp->Clear();
       $vkeyword=gb2utf($vkeyword);
       $vkeyword=explode(" ",$vkeyword);
       $vkeyword=implode(",",$vkeyword);
       $xiabiao=strrpos($vkeyword,",");
       $vkeyword=substr($vkeyword,0,$xiabiao) ;
       $this->assign('vkeyword',$vkeyword);

            $vpage['content']=$vs['content'];

            $vdes=$vpage['content'];
            $vdes=strip_tags($vdes);
            $vdes=msubstr($vdes,150);
            $vdes=trim($vdes);
            $vdes=str_replace("\t","",$vdes);
            $vdes=str_replace("\r","",$vdes);
            $vdes=str_replace("\n","",$vdes);
            $vdes=str_replace("　　","",$vdes);
            $vdes=str_replace("&nbsp;","",$vdes);
            //echo $vdes;exit;



            //import ( '@.ORG.Contentpage' );
            //$cm=new Contentpage();
            //$vpage=$cm->ContentsPages(__APP__."/Index/show/id/".$vo['id'],$vo['content'],$ptext = '#page#');
            //dump($vpage['total']);






				$this->assign('vprev',$vprev);
                $this->assign('vnext',$vnext);
                //$vpage['content']=preg_replace("/小号/i","<a  href=\"http://www.xiaohao.net\">小号</a>",$vpage['content'],1);
                $vpage['content']=str_replace('alt=""',"",$vpage['content']);
                $vpage['content']=str_replace("<img","<img alt=\"".$vs['title']."\" ",$vpage['content']);

                $zheng=$vpage['content'];
                $zheng = str_replace("\n", '', $zheng);
                $zheng = str_replace("\r", '', $zheng);
                $zheng = str_replace("<br />", '', $zheng);
                $zheng = str_replace("<br>", '', $zheng);
                $luan=$vpage['content'];
                
                
                if($vs['fan'] == 1){
                $luan=strip_tags($luan,"<a><img>");
                $luan=explode("。",$luan);
                shuffle($luan);
                $luan=implode("",$luan);
            }
                
                
                $this->assign('luan',$luan);
                $this->assign('zheng',$zheng);


               // dump($vpage['content']);exit;

                $this->assign('vo',$vs);
                $this->assign('rs',$rs);
                $this->assign('vpage',$vpage);
                $this->assign('vdes',$vdes);



				$this->display('article');






        }
        else
        {

            exit('页面不存在！');

        }

	}


    //
	public function zheng()
    {
        //echo $_GET['id'];
        $id=RemoveXSS($_GET['id']);
        if(!empty($id))
        {


            $type= M("article");

            $vs= $type->where(" id = ".$id)->find();

            $vpage['content']=$vs['content'];

                //$vpage['content']=preg_replace("/小号/i","<a  href=\"http://www.xiaohao.net\">小号</a>",$vpage['content'],1);
                $vpage['content']=str_replace('alt=""',"",$vpage['content']);
                $vpage['content']=str_replace("<img","<img alt=\"".$vs['title']."\" ",$vpage['content']);
                echo $vpage['content'];

        }
        else
        {

            exit('页面不存在！');

        }

	}

    //验证码
    public function verify()
    {
		$type	 =	 isset($_GET['type'])?$_GET['type']:'gif';
        import("@.ORG.Image");
        Image::buildImageVerify(4,1,$type);
    }

    //搜索
    public function search()
    {


             $query=RemoveXSS($_GET['q']);
             
             $type = M ('article');
             //热点文章
            $rd=$type->order("click desc")->limit("10")->findAll();
            $this->assign("rd",$rd);
            $this->assign("title","搜索页面");

             if($query!=''){


             if($_COOKIE["search_time"] == "")
		{
			 $Comm= M ('article');

            //import("@.ORG.Page"); //导入分页类
			//$count = $Comm->where("title like '%".$query."%'")->count();   //计算总数
			//$p = new Page ( $count , 2 );
            
            import("@.ORG.Page"); //导入分页类
            $count = $Comm->where("title like '%".$query."%'")->count();   //计算总数
            $p = new Page ( $count , 10);
            $rs=$Comm->where("title like '%".$query."%'")->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();

            $page = $p->show ("/s-".$query);
            $this->assign('page',$page);
            
            
            
            
			//$rs=$Comm->where("title like '%".$query."%'")->order('id desc')->findAll();

            setcookie("search_time",time());
			//$page = $p->show ("http://".C('URL')."/search/".$query."/");


            $this->assign('rs',$rs);
		    $this->display('search');
		}
        else
        {



            if(time() - $_COOKIE["search_time"] > 1)
			{




            $Comm= M ('article');

            //import("@.ORG.Page"); //导入分页类
			//$count = $Comm->where("title like '%".$query."%'")->count();   //计算总数
			//$p = new Page ( $count , 2 );
            import("@.ORG.Page"); //导入分页类
			$count = $Comm->where("title like '%".$query."%'")->count();   //计算总数
            $p = new Page ( $count , 10);
            $rs=$Comm->where("title like '%".$query."%'")->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();

            $page = $p->show ("/s-".$query);
            $this->assign('page',$page);

            setcookie("search_time",time());

			//$page = $p->show ("http://".C('URL')."/search/".$query."/");


            $this->assign('rs',$rs);
		    $this->display('search');
            }
            else
            {
                setcookie("search_time",time());
                $this->assign('msg','歇30秒再搜');
		        $this->display('search');

            }

        }


             }else{


                setcookie("search_time",time());
                $this->assign('msg','请输入关键词');
		        $this->display('search');


        }



    }

    //插入评论
    public function insertcomment()
    {
        $iph="";
if(isset($_SERVER['HTTP_CLIENT_IP'])){
$iph=$_SERVER['HTTP_CLIENT_IP'];
}else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$iph=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$iph=$_SERVER['REMOTE_ADDR'];
}


        $comm	=	M("comments");

        $ip=$comm->limit('0'.','.'1')->order('id desc')->getByIp($iph);


        if($ip['ip']!='')
        {
            if(time()-$ip['time']<3)
            {
               //$this->error('歇30秒再评论');
                echo "<script>alert('歇三十秒再进行评论');window.history.back(-1);</script>";
                return false;

            }

        }

         if($_POST['name'] =='') {
                echo "<script>alert('昵称不能为空');window.history.back(-1);</script>";
                 return false;
        }





			if($comm->where(" name = '".RemoveXSS($_POST['name'])."'")->find()) {
               // echo $comm->getlastsql();exit;
				echo "<script>alert('昵称已经存在');window.history.back(-1);</script>";
                 return false;

            }

            if($_POST['content'] =='') {
                echo "<script>alert('内容不能为空');window.history.back(-1);</script>";
                 return false;
        }



       if(strlen($_POST['content'])>1000)
        {
           echo "<script>alert('内容过长，别超过500字');window.history.back(-1);</script>";
            return false;

       }




     if($_SESSION['verify'] != md5($_POST['verify'])) {
                echo "<script>alert('验证码错误');window.history.back(-1);</script>";
                 return false;
        }




           $data['name']=RemoveXSS($_POST['name']);
           $data['content']=strip_tags(RemoveXSS($_POST['content']));
           $data['aid']=RemoveXSS($_POST['aid']);
           $data['time']=time();
           $data['ip']=$iph;
           $status=1;

           if($comm->data($data)->add())
           {
               $artDao=M("article");
               $tsid=$artDao->where(" id =  ".RemoveXSS($_POST['aid']))->field("tid")->find();
               if(file_exists('./ns/Html/content_'.$_POST['aid'].'.html')){

                   @unlink('./ns/Html/content_'.$_POST['aid'].'.html');

               }

               echo"<script>alert('发表评论成功!');window.location.href='http://".C("URL")."/".getfenlei($tsid['tid'])."/".RemoveXSS($_POST['aid'])."'</script>";

           }
           else
          {
                echo"<script>alert('发表评论失败!');window.location.href='http://".C("URL")."/".getfenlei($tid['tid'])."/".RemoveXSS($_POST['aid'])."'</script>";

           }



    }

    public function shop() {

        echo "正在制作，敬请期待！";

    }

    public function sitemap() {

        $this->display('sitemap');

    }


    //列表页
   public function lista()
   {
         //echo F('list_count_data'.$id['id']."".$_GET['p']);exit;
            $id=RemoveXSS($_GET['cc']);
            $typeDao=M("type");
            $Comm= M ('article');
            //dump($id['id']);
            import("@.ORG.Page"); //导入分页类
            $count = $Comm->where("tid=".$id)->count();   //计算总数
            $p = new Page ( $count , 15);
            $rs=$Comm->where("tid=".$id)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();

           

            $page = $p->show ("/t-".$id);
            $this->assign('page',$page);
            $this->assign('rs',$rs);
            $this->assign('nav',$id);

            $type=M("article");

       

            //热点文章
            $rd=$type->where(" tid = ".$id)->order("click desc")->limit("10")->findAll();
            $this->assign("rd",$rd);

     

            if($id==19){

                $this->assign("title","犯罪心理_脑髓地狱");
                $this->assign("keywords","犯罪心理，犯罪心理学，犯罪心理的研究，青少年犯罪心理");
                $this->assign("des","研究犯罪心理学范畴内的一切，研究犯罪行为的因果来源，预防犯罪。");

            }else if($id==20){

                $this->assign("title","法医和现场_脑髓地狱");
                $this->assign("keywords","法医，女法医，法医学，我是一名法医");
                $this->assign("des","以一名法医的视角审视犯罪现场，解剖尸体，探寻蛛丝马迹，用技术将凶犯绳之于法！");

            }else if($id==21){
                $this->assign("title","杀手与谋杀_脑髓地狱");
                $this->assign("keywords","杀手，谋杀，冷血，谋杀的艺术");
                $this->assign("des","给你最权威的杀手集合，谋杀，又是谋杀，有人的地方，就有谋杀");

            }else if($id==22){

                $this->assign("title","魔鬼的梦魇_脑髓地狱");
                $this->assign("keywords","噩梦，痛苦，梦魇，痛不欲生，魔鬼");
                $this->assign("des","探索痛苦之源，是什么让你痛不欲生？是梦魇的折磨还是魔鬼的调戏？");

            }else if($id==23){

                $this->assign("title","重口味_脑髓地狱");
                $this->assign("keywords","重口味，重口味剧集，重口味电影，重口味小说，给你这样的重口味");
                $this->assign("des","重口味患者集中营，秀出变态的下限，证明重口味无罪。");

            }




           
            $this->display('list');
     




   }
   
   
   
   //标签页
   public function tag()
   {
         //echo F('list_count_data'.$id['id']."".$_GET['p']);exit;
            $name=RemoveXSS($_GET['cc']);
            $typeDao=M("tag");
            $tid = $typeDao->where(" name = '".$name."' ")->find();
            $typeDao=M("tag_article");
            
            $tida = $typeDao->where(" t_id = ".$tid['id'])->findAll();
            
            $tids = array();
            
            foreach($tida as $value){
            
                    if($value){
                    
                    $tids[]=$value['a_id'];
                }
            
            }
            
            $tidss = implode(",",$tids);
            
            $Comm= M ('article');
            //dump($id['id']);
            import("@.ORG.Page"); //导入分页类
            $count = $Comm->where(" id in (".$tidss.")  ")->count();   //计算总数
            $p = new Page ( $count , 15);
            $rs=$Comm->where(" id in (".$tidss.")  ")->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();

           

            $page = $p->show ("/tag=".$name);
            $this->assign('page',$page);
            $this->assign('rs',$rs);
            $this->assign('nav',$id);

            $type=M("article");

       

            //热点文章
            $rd=$type->order("click desc")->limit("10")->findAll();
            $this->assign("rd",$rd);

     

           
                $this->assign("title",$name);
         




           
            $this->display('tag');
     




   }
   
   
   
   

}
?>
